package com.open.loan.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.open.loan.utils.HttpUtil;
import com.open.loan.utils.ResponseUtil;
import com.open.system.domain.LoanApplyDO;
import com.open.system.domain.LoanBankDO;
import com.open.system.domain.LoanJuxinliReportDO;
import com.open.system.domain.LoanJuxinliTokenDO;
import com.open.system.domain.LoanUserDO;
import com.open.system.service.LoanApplyService;
import com.open.system.service.LoanBankService;
import com.open.system.service.LoanJuxinliReportService;
import com.open.system.service.LoanJuxinliTokenService;
import com.open.system.service.LoanUserService;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/moblie")
public class VueJuXinliGetController {
	
	@Autowired
	private LoanJuxinliTokenService loanJuxinliTokenService;
	
	@Autowired
	private LoanUserService loanUserService;
	
	@Autowired
	private LoanBankService loanBankService;
	
	@Autowired
	private LoanApplyService loanApplyService;
	
	@Autowired
	private LoanJuxinliReportService loanJuxinliReportService;
	
	@ApiOperation(value="查询报告", notes="")
	@RequestMapping(value ="/getReport",method = RequestMethod.GET)
	public ResponseEntity<String> getReport(HttpServletRequest request,HttpServletResponse response){
				try {
					String returnCode="200";
					HttpSession session = request.getSession();
					LoanUserDO loanUserDo=(LoanUserDO) session.getAttribute("loanUserDo");
					if(loanUserDo==null){
						ResponseUtil.toWrite("100","请重新登录",null, response);
						return null;
					}
					Map<String, Object> bankParams=new HashMap<String, Object>();
					bankParams.put("userId",loanUserDo.getId());
					LoanBankDO bankDo=null;
					List<LoanBankDO>bankLists=loanBankService.list(bankParams);
					for (int i = 0; i <bankLists.size(); i++) {
						bankDo=bankLists.get(0);
					}
					if(bankDo==null){
						ResponseUtil.toWrite("101","请先认证银行卡",null, response);
						return null;
					}
					Map<String, Object> tokenParam=new HashMap<String, Object>();
					List<LoanJuxinliTokenDO>tokenDOs=loanJuxinliTokenService.list(tokenParam);
					LoanJuxinliTokenDO tokenDO=new LoanJuxinliTokenDO();
					for (int i = 0; i <tokenDOs.size(); i++) {
						tokenDO=tokenDOs.get(0);
					}
					
					//判断数据库是否有值--开始--
					Map<String, Object> reportParam=new HashMap<String, Object>();
					reportParam.put("userId",loanUserDo.getId());
					List<LoanJuxinliReportDO>reportDOs=loanJuxinliReportService.list(reportParam);
					if(reportDOs!=null && reportDOs.size()>0){
						ResponseUtil.toWrite("102","您已认证过了",null, response);
						return null;
					}
					//判断数据库是否有值--结束--
					String url="https://mi.juxinli.com/api/search";
					Map<String, String> params=new HashMap<String, String>();
					params.put("client_secret",tokenDO.getClientSecret());
					params.put("access_token",tokenDO.getAccessToken());
					params.put("name",bankDo.getRealName());
					params.put("id_card",bankDo.getIdNo());
					params.put("phone",bankDo.getMoblie());
					params.put("version","v3");
					String returnJson=HttpUtil.sendGet(url, params);
					JSONObject jsonObject =JSONObject.parseObject(returnJson);
					String code=jsonObject.getString("code");
					String message=jsonObject.getString("message");
					
					if("MIGUAN_SEARCH_SUCCESS".equals(code)){
						returnCode="200";
						String data=jsonObject.getString("data");
						JSONObject jsonData =JSONObject.parseObject(data);
						String user_grid_id=jsonData.getString("user_grid_id");//1
						String iou_statistic=jsonData.getString("iou_statistic");//2
						String user_gray=jsonData.getString("user_gray");//3
						
						
						JSONObject jsonUser_gray =JSONObject.parseObject(user_gray);
						String contacts_number_statistic=jsonUser_gray.getString("contacts_number_statistic");//4
						String contacts_rfm=jsonUser_gray.getString("contacts_rfm");//5
						String contacts_closest=jsonUser_gray.getString("contacts_closest");//6
						String contacts_gray_score=jsonUser_gray.getString("contacts_gray_score");//7
						String contacts_relation_distribution=jsonUser_gray.getString("contacts_relation_distribution");//8
						String contacts_query=jsonUser_gray.getString("contacts_query");//9
						
						
						String user_searched_statistic=jsonData.getString("user_searched_statistic");//10
						String user_register_orgs=jsonData.getString("user_register_orgs");//11
						String user_blacklist=jsonData.getString("user_blacklist");//12
						String user_basic=jsonData.getString("user_basic");//13
						String auth_org=jsonData.getString("auth_org");//14
						String user_phone_suspicion=jsonData.getString("user_phone_suspicion");//15
						String user_idcard_suspicion=jsonData.getString("user_idcard_suspicion");//16
						String user_searched_history_by_orgs=jsonData.getString("user_searched_history_by_orgs");//17
						String user_batch_searched_history_by_orgs=jsonData.getString("user_batch_searched_history_by_orgs");//18
						String user_searched_history_by_day=jsonData.getString("user_searched_history_by_day");//19
						String consumer_label=jsonData.getString("consumer_label");//20
						
						LoanJuxinliReportDO reportDO=new LoanJuxinliReportDO();
						reportDO.setCode(code);
						reportDO.setMessage(message);
						reportDO.setUserId(loanUserDo.getId());
						reportDO.setCreateTime(new Date());
						reportDO.setPhone(bankDo.getMoblie());
						reportDO.setRealName(bankDo.getRealName());
						reportDO.setJsondata(data);
						reportDO.setUserGridId(user_grid_id);
						reportDO.setIouStatistic(iou_statistic);
						reportDO.setUserGray(user_gray);
						reportDO.setUserGrayContactsNumberStatistic(contacts_number_statistic);
						reportDO.setUserGrayContactsRfm(contacts_rfm);
						reportDO.setUserGrayContactsClosest(contacts_closest);
						reportDO.setUserGrayContactsGrayScore(contacts_gray_score);
						reportDO.setUserGrayContactsRelationDistribution(contacts_relation_distribution);
						reportDO.setUserGrayContactsQuery(contacts_query);
						reportDO.setUserSearchedStatistic(user_searched_statistic);
						reportDO.setUserRegisterOrgs(user_register_orgs);
						reportDO.setUserBlacklist(user_blacklist);
						reportDO.setUserBasic(user_basic);
						reportDO.setAuthOrg(auth_org);
						reportDO.setUserPhoneSuspicion(user_phone_suspicion);
						reportDO.setUserIdcardSuspicion(user_idcard_suspicion);
						reportDO.setUserSearchedHistoryByOrgs(user_searched_history_by_orgs);
						reportDO.setUserBatchSearchedHistoryByOrgs(user_batch_searched_history_by_orgs);
						reportDO.setUserSearchedHistoryByDay(user_searched_history_by_day);
						reportDO.setConsumerLabel(consumer_label);
						loanJuxinliReportService.save(reportDO);
						/*JSONObject jsonUser_basic =JSONObject.parseObject(data);
						//字段
						String user_idcard=jsonUser_basic.getString("user_idcard");
						String user_province=jsonUser_basic.getString("user_province");
						String user_city=jsonUser_basic.getString("user_city");
						String user_region=jsonUser_basic.getString("user_region");
						String user_idcard_valid=jsonUser_basic.getString("user_idcard_valid");//身份证是否有效 true
						String user_age=jsonUser_basic.getString("user_age");
						String user_gender=jsonUser_basic.getString("user_gender");
						String user_name=jsonUser_basic.getString("user_name");
						String user_phone=jsonUser_basic.getString("user_phone");
						String user_phone_operator=jsonUser_basic.getString("user_phone_operator");//运营商类别
						String user_phone_province=jsonUser_basic.getString("user_phone_province");
						String user_phone_city=jsonUser_basic.getString("user_phone_city");*/
						//基本信息-----------------结束---
						//申请认证修改------开始
						LoanApplyDO applyDO=new LoanApplyDO();
						Map<String, Object> map=new HashMap<String, Object>();
						map.put("userId",loanUserDo.getId());
						List<LoanApplyDO>applyDOs=loanApplyService.list(map);
						for (int i = 0; i <applyDOs.size(); i++) {
							applyDO=applyDOs.get(0);
						}
						applyDO.setCreditAuth("2");;//['0待完善', '1审核中', '2认证成功', '3认证失败']
						loanApplyService.update(applyDO);
						//申请认证修改------结束
					}else {
						returnCode="202";
					}
					System.out.println(code);
					ResponseUtil.toWrite(returnCode,code,null, response);
					return null;
				} catch (Exception e) {
					e.printStackTrace();
					ResponseUtil.toWrite("202","获取数据失败",null, response);
					return null;
				}
		}
	
	@ApiOperation(value="测试", notes="")
	@RequestMapping(value ="/getTest",method = RequestMethod.GET)
	public ResponseEntity<String> getTest(HttpServletRequest request,HttpServletResponse response){
				try {
					String url="https://mi.juxinli.com/api/search";
					Map<String, String> params=new HashMap<String, String>();
					params.put("client_secret","a0d730a9ebb4411f881b52f5e6d5361a");
					params.put("access_token","6aa02e8304cc4ed0abe34b111bf5354f");
					params.put("name","");
					params.put("id_card","");
					params.put("phone","");
					params.put("version","v3");
					String returnJson=HttpUtil.sendGet(url, params);
					JSONObject jsonObject =JSONObject.parseObject(returnJson);
					String code=jsonObject.getString("code");
					String message=jsonObject.getString("message");
					
					if("MIGUAN_SEARCH_SUCCESS".equals(code)){
						String data=jsonObject.getString("data");
						JSONObject jsonData =JSONObject.parseObject(data);
						String user_grid_id=jsonData.getString("user_grid_id");//1
						String iou_statistic=jsonData.getString("iou_statistic");//2
						String user_gray=jsonData.getString("user_gray");//3
						
						
						JSONObject jsonUser_gray =JSONObject.parseObject(user_gray);
						String contacts_number_statistic=jsonUser_gray.getString("contacts_number_statistic");//4
						String contacts_rfm=jsonUser_gray.getString("contacts_rfm");//5
						String contacts_closest=jsonUser_gray.getString("contacts_closest");//6
						String contacts_gray_score=jsonUser_gray.getString("contacts_gray_score");//7
						String contacts_relation_distribution=jsonUser_gray.getString("contacts_relation_distribution");//8
						String contacts_query=jsonUser_gray.getString("contacts_query");//9
						
						
						String user_searched_statistic=jsonData.getString("user_searched_statistic");//10
						String user_register_orgs=jsonData.getString("user_register_orgs");//11
						String user_blacklist=jsonData.getString("user_blacklist");//12
						String user_basic=jsonData.getString("user_basic");//13
						String auth_org=jsonData.getString("auth_org");//14
						String user_phone_suspicion=jsonData.getString("user_phone_suspicion");//15
						String user_idcard_suspicion=jsonData.getString("user_idcard_suspicion");//16
						String user_searched_history_by_orgs=jsonData.getString("user_searched_history_by_orgs");//17
						String user_batch_searched_history_by_orgs=jsonData.getString("user_batch_searched_history_by_orgs");//18
						String user_searched_history_by_day=jsonData.getString("user_searched_history_by_day");//19
						String consumer_label=jsonData.getString("consumer_label");//20
						
						LoanJuxinliReportDO reportDO=new LoanJuxinliReportDO();
						reportDO.setCode(code);
						reportDO.setMessage(message);
						reportDO.setUserId(24L);
						reportDO.setCreateTime(new Date());
						reportDO.setPhone("15159777928");
						reportDO.setRealName("潘惠贤");
						reportDO.setJsondata(data);
						reportDO.setUserGridId(user_grid_id);
						reportDO.setIouStatistic(iou_statistic);
						reportDO.setUserGray(user_gray);
						reportDO.setUserGrayContactsNumberStatistic(contacts_number_statistic);
						reportDO.setUserGrayContactsRfm(contacts_rfm);
						reportDO.setUserGrayContactsClosest(contacts_closest);
						reportDO.setUserGrayContactsGrayScore(contacts_gray_score);
						reportDO.setUserGrayContactsRelationDistribution(contacts_relation_distribution);
						reportDO.setUserGrayContactsQuery(contacts_query);
						reportDO.setUserSearchedStatistic(user_searched_statistic);
						reportDO.setUserRegisterOrgs(user_register_orgs);
						reportDO.setUserBlacklist(user_blacklist);
						reportDO.setUserBasic(user_basic);
						reportDO.setAuthOrg(auth_org);
						reportDO.setUserPhoneSuspicion(user_phone_suspicion);
						reportDO.setUserIdcardSuspicion(user_idcard_suspicion);
						reportDO.setUserSearchedHistoryByOrgs(user_searched_history_by_orgs);
						reportDO.setUserBatchSearchedHistoryByOrgs(user_batch_searched_history_by_orgs);
						reportDO.setUserSearchedHistoryByDay(user_searched_history_by_day);
						reportDO.setConsumerLabel(consumer_label);
						loanJuxinliReportService.save(reportDO);
					}
					System.out.println(code);
					ResponseUtil.toWrite("200",code,null, response);
					return null;
				} catch (Exception e) {
					e.printStackTrace();
					ResponseUtil.toWrite("202","获取数据失败",null, response);
					return null;
				}
		}
	
}
